﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.QingJia;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.QingJia
{
    public class QingJiaQueryCommandHandler : IRequestHandler<QingJiaQueryCommand, APIPanging<QingJiaDto>>
    {
        private readonly IBaseRepository<LeaveApplicationForm> lRepo;
        private readonly IMapper mapper;
        private readonly IBaseRepository<User> uRepo;
        private readonly IBaseRepository<RecordModel> recRepo;

        public QingJiaQueryCommandHandler(IBaseRepository<LeaveApplicationForm> lRepo, IMapper mapper, IBaseRepository<User> uRepo, IBaseRepository<RecordModel> recRepo)
        {
            this.lRepo = lRepo;
            this.mapper = mapper;
            this.uRepo = uRepo;
            this.recRepo = recRepo;
        }

        public async Task<APIPanging<QingJiaDto>> Handle(QingJiaQueryCommand request, CancellationToken cancellationToken)
        {
            APIPanging<QingJiaDto> result = new APIPanging<QingJiaDto>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            //当前用户信息
            var users = request.HaoId;

            var list = from a in lRepo.GetAll().Result
                       join b in uRepo.GetAll().Result on a.DepartName equals b.UserId
                       where a.IsDeleted == false
                       select new QingJiaDto
                       {
                           FormId = a.FormId,
                           UserName = a.UserName,
                           DepartId =a.DepartId,
                           LeaveCategory = a.LeaveCategory,
                           EndTime = a.EndTime,
                           BaoDaoTime = a.BaoDaoTime,
                           CreateTime = a.CreateTime,
                           Zongtime = a.Zongtime,
                           LeaveReason = a.LeaveReason, 
                           DepartName = a.DepartName,
                           Status = a.Status,
                           IsDeleted = a.IsDeleted,
                           BeginTime = a.BeginTime,
                           ApproveReson=a.ApproveReson,
                           DepName = b.UserName,//部门领导名称
                           HaoId = b.HaoId-1,
                       };
            if (users != 0)//普通员工
            {
               list = list;
            }
            else { 
                list = list.Where(x => x.UserName==request.UserName) ;
               
            }
            if (request.BeginTime != null && request.EndTime != null)
            {
                list = list.Where(x => x.BeginTime >= request.BeginTime.Value && x.EndTime <= request.EndTime.Value);
            }
            if (request.Status!=null)
            {
                list = list.Where(x=>x.Status==request.Status); 
            }
          
          


            int TotalCount = list.Count();
            int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
            list = list.OrderByDescending(x=>x.FormId).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize);          
            result.TotalCount = TotalCount;  
            result.TotalPage = TotalPage;
            result.List = mapper.Map<List<QingJiaDto>>(list.ToList());
            return await Task.FromResult(result);




        }



    }
}
